<?php

#
# vanlig conffil i form av: variabel = värde
#
function readConf( $file ) {
        $file = file( $file );
        $settings = array();
        foreach ($file as $line) {
                trim($line);
                if($line[0] != '#') {
                        $words = explode(" ", $line);
                        if(isset($words[1]) AND trim($words[1]) == '=') {
                                $key = $words[0];
                                $value = implode(" ", array_slice($words, 2));
                                $settings[$key] = trim($value);
                        }
                }
        }
        return $settings;
}


function writeConf( $file, $settings ) {
	$fp = fopen($file, "w");
	foreach($settings as $key => $value) {
		fwrite($fp, "$key = $value\n");
	}
	fclose($fp);
}


#
# INI-file
#
function readINIfile ($filename, $commentchar) {
	$array1 = @file($filename);
	if($array1 == false)
		return false;
	$section = '';
	foreach ($array1 as $filedata) {
		$dataline = trim($filedata);
		$firstchar = substr($dataline, 0, 1);
		if ($firstchar!=$commentchar && $dataline!='') {
			//om verkligen data
			if ($firstchar == '[' && substr($dataline, -1, 1) == ']') {
				//section
				$section = strtolower(substr($dataline, 1, -1));
			}
			else {
				//nyckel
				$delimiter = strpos($dataline, '=');
				if ($delimiter > 0) {
					//värde
					$key = strtolower(trim(substr($dataline, 0, $delimiter)));
					$value = trim(substr($dataline, $delimiter + 1));
					if (substr($value, 0, 1) == '"' && substr($value, -1, 1) == '"') { 
						$value = substr($value, 1, -1);
					}
					$array2[$section][$key] = stripcslashes($value);
				}
				else {
					//inget värde
					$array2[$section][strtolower(trim($dataline))]='';
				}
			}
		}
	}
	return $array2;
}

function writeINIfile ($filename, $array1, $commentchar, $commenttext) {
  $handle = fopen($filename, 'wb');
  if ($commenttext!='') {
   $comtext = $commentchar.
     str_replace($commentchar, "\r\n".$commentchar,
       str_replace ("\r", $commentchar,
         str_replace("\n", $commentchar,
           str_replace("\n\r", $commentchar,
             str_replace("\r\n", $commentchar, $commenttext)
           )
         )
       )
     )
   ;
   if (substr($comtext, -1, 1)==$commentchar && substr($comtext, -1, 1)!=$commentchar) {
     $comtext = substr($comtext, 0, -1);
   }
   fwrite ($handle, $comtext."\r\n");
  }
  foreach ($array1 as $sections => $items) {
   //Write the section
   if (isset($section)) { fwrite ($handle, "\r\n"); }
   //$section = ucfirst(preg_replace('/[\0-\37]|[\177-\377]/', "-", $sections));
   $section = preg_replace('/[\0-\37]|\177/', "-", $sections);
   fwrite ($handle, "[".$section."]\r\n");
   foreach ($items as $keys => $values) {
     //Write the key/value pairs
     //$key = ucfirst(preg_replace('/[\0-\37]|=|[\177-\377]/', "-", $keys));
     $key = preg_replace('/[\0-\37]|=|\177/', "-", $keys);
     if (substr($key, 0, 1)==$commentchar) { $key = '-'.substr($key, 1); }
     $value = addcslashes($values,'');
     fwrite ($handle, '    '.$key.' = "'.$value."\"\r\n");
   }
  }
  fclose($handle);
}

?>
